import ajax from './ajax'
import store from '@/store'

// 获取token
export function getAPIToken(authCode) {
	return new Promise((resolve, reject) => {
		ajax({
			method: 'get',
			url: '/outing/mobile/login',
			data: {
				code: authCode,
				corpId: dd.corpId
			}
		}).then(res => {
			// console.log("res", res)
			// 将token保存到store中
			store.commit("changeToken", res.data)
			resolve(res)
		}).catch(e => {
			reject(e)
		})
	})
}
// 免密登陆
export function passwordFreeLogin() {
	return new Promise((resolve, reject) => {
		// 获取钉钉免登码登陆
		dd.getAuthCode({
			success: (res) => {
				// console.log('huoqucode', res.authCode, dd.corpId)
				if (res.authCode) {
					getAPIToken(res.authCode).then(res => {
						resolve(res)
					}).catch(e => {
						reject(e)
					})
				}
			},
			fail: (err) => {
				console.log("获取免登码错误", err)
				uni.showToast({
					title: `${err.errorMessage}`,
					icon: 'none'
				});
				reject(err)
			}
		});
	})
}
